from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:123@localhost:3306/db_phone_analysis_data_1?charset=utf8'

db = SQLAlchemy(app)


class tbl_phone_brand_count(db.Model):
    __tablename__ = 'tbl_phone_brand_count'
    fld_index = db.Column(db.Integer, primary_key=True)
    fld_brand_name = db.Column(db.VARCHAR(256))
    fld_sale_count = db.Column(db.Integer)


class tbl_phone_color_count(db.Model):
    __tablename__ = 'tbl_phone_color_count'
    fld_index = db.Column(db.Integer, primary_key=True)
    fld_phone_color = db.Column(db.VARCHAR(256))
    fld_sale_count = db.Column(db.Integer)


class tbl_phone_size_count(db.Model):
    __tablename__ = 'tbl_phone_size_count'
    fld_index = db.Column(db.Integer, primary_key=True)
    fld_phone_size = db.Column(db.VARCHAR(256))
    fld_sale_count = db.Column(db.Integer)


@app.route("/")
def index():
    data1 = tbl_phone_brand_count.query.order_by('fld_sale_count desc').all()
    data2 = tbl_phone_color_count.query.all()
    data3 = tbl_phone_size_count.query.all()
    return render_template('index.html', data1=data1, data2=data2, data3=data3)


if __name__ == '__main__':
    app.run(debug=True)
